package edu.xupt.ediction.slice;

import edu.xust.ediction.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

public class SignAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sign);

        //获取用户名
        TextField tf_account =(TextField)this.findComponentById(ResourceTable.Id_text_accountz);
//        System.out.print("输出结果");
//        System.out.print("---------"+tf_account);
        //获取密码
        TextField tf_password =(TextField)this.findComponentById(ResourceTable.Id_text_passwordz);
        //获取确认密码
        TextField tf_repassword =(TextField)this.findComponentById(ResourceTable.Id_text_repassword);
        //  获取返回按钮
        Button button_return = (Button) this.findComponentById(ResourceTable.Id_btn_return);

        //注册返回按钮跳转到登录界面
        Image sign_return=(Image)this.findComponentById(ResourceTable.Id_sign_return);
        sign_return.setClickedListener(component ->present(new MainAbilitySlice(),new Intent()));

        //注册信息按钮跳转到注册详情界面
        Image signinfo_zc=(Image)this.findComponentById(ResourceTable.Id_result_zc);
        signinfo_zc.setClickedListener(component ->present(new RegisterAbilitySlice(),new Intent()));

        //获取注册按钮
        Button button_sign = (Button) this.findComponentById(ResourceTable.Id_btn_signup);
        button_sign.setClickedListener(component -> {
            String name=tf_account.getText();
            String passwd=tf_password.getText();
            String repasswd=tf_repassword.getText();
            if("".equals(name)||name==null){
                new ToastDialog(getAbility()).setContentText("用户名不能为空").setDuration(2500).show();
            }else if("".equals(passwd)||passwd==null){
                new ToastDialog(getAbility()).setContentText("密码不能为空").setDuration(2500).show();
            }else if(!passwd.equals(repasswd)){
                new ToastDialog(getAbility()).setContentText("密码不一致！").setDuration(2500).show();
            }else {
                new ToastDialog(getAbility()).setContentText("注册成功！").setDuration(2500).show();
               // present(new MainAbilitySlice(),new Intent());
                button_return.setClickedListener(this::ClickedListenerButtonReturn);
            }
        });
    }

    private void ClickedListenerButtonReturn(Component component) {
        // 同一个Ability里页面之间的跳转
        Intent intent = new Intent();
        this.present(new MainAbilitySlice(),intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
